<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/static/assets/css/layui.css">
    <title>添加IC卡信息</title>
</head>
<body>
<div class="layui-content">
    <div class="layui-row">
        <div class="layui-card">
            <form  class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="ownerId" id="ownerId" required=""  lay-verify="number|required" placeholder="请输入用户ID" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">车牌号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="carNum" id="carNum" required="" lay-verType="tips"  lay-verify="required|carNum" placeholder="请输入车牌号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">停车位</label>
                    <div class="layui-input-inline">
                        <select name="parkPlaceId" id="parkPlaceNum" lay-filter="filter" required="" lay-verify="required">
                            <option value=""></option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="/static/assets/layui.all.js"></script>
<script src="/static/assets/jquery.min.js"></script>
<script src="/static/assets/limit.js"></script>
<script>
    $(function () {
        layui.use('form', function(){
                var form = layui.form
                    ,layer = layui.layer;
                form.verify({
                    carNum:[
                        /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/
                        ,'车牌号格式不正确！'
                    ]
                });
                $.ajax({
                    url:"/parkPlace/selectAllFixed",
                    type:"post",
                    dataType:"json",
                    data:{},
                    success:function (res) {
                        console.log(res)
                        for (var i = 0;i<res.data.parkPlaceNum.length;i++){
                            $("#parkPlaceNum").append("<option value='"+res.data.parkPlaceId[i]+"'>"+res.data.parkPlaceNum[i]+"</option>")
                            form.render()
                        }
                        var ownerId =  $("#ownerId")
                        ownerId.on("change",(function () {
                            $.ajax({
                                url:"/ICCard/selectByOwnerId",
                                type:"post",
                                dataType:"json",
                                data:{
                                    ownerId:ownerId.val()
                                },
                                success:function (res) {
                                    console.log(res)
                                    if(res.code!==200){
                                        layer.msg("不存在这个用户")
                                    }else {
                                        layer.msg("用户可用")
                                    }
                                },
                                error:function (data) {

                                }
                            })
                        }))

                        form.on('submit(add)', function(data){
                            // data.field.parkPlaceNum = parkPlaceNum
                            console.log(data)
                            layer.confirm('您确定要增加IC卡吗？', {
                                btn: ['确定','取消'] //按钮
                            }, function(){
                                $.ajax({
                                        url:"/ICCard/add",
                                        type:"post",
                                        dataType:"json",
                                        data:data.field,
                                        success:function (res) {
                                            if(res.code===200){
                                                layer.msg("添加成功")
                                                setTimeout(function(){
                                                    window.parent.location.reload(); //刷新父页面
                                                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                                    parent.layer.close(index);  // 关闭layer
                                                },1000);
                                            }else {
                                                layer.msg("添加失败")
                                            }

                                        },error:function (data) {

                                        }
                                    })
                            }, function() {
                                layer.msg('已取消', {
                                    time: 2000 //20s后自动关闭
                                })
                            })
                            return false;
                        });
                    },
                    error:function (data) {

                    }
                })
            });
    })

</script>

</body>
</html>
